package org.gruposp2p.proyectokarma.gwt.client.cometd;

import java.util.ArrayList;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import org.gruposp2p.proyectokarma.gwt.client.domain.CometdMessage;
import org.gruposp2p.proyectokarma.gwt.client.domain.KarmaParameter;
import org.gruposp2p.proyectokarma.gwt.client.event.CometdEvent;
import org.gruposp2p.proyectokarma.gwt.client.event.EventBusManager;
import org.gruposp2p.proyectokarma.gwt.client.ui.ErrorDialog;
import org.gruposp2p.proyectokarma.gwt.client.util.DateUtils;
import org.gruposp2p.proyectokarma.gwt.client.util.RequestHelper;
import org.gruposp2p.proyectokarma.gwt.client.util.ServerPaths;
import com.allen_sauer.gwt.log.client.Log;
	
public class MessagesTablePanel extends Composite 
	implements CometdEvent.Handler {

    @UiField CellTable<CometdMessage> cellTable;
    @UiField SimplePager simplePager;
    CometdMessageListAsyncAdapter adapter;
    private int offset = 0;
    private int cellTablePageSize = 20;
    
    ArrayList<CometdMessage> values;

    private static CometdMessagesTablePanelUiBinder uiBinder = 
    	GWT.create(CometdMessagesTablePanelUiBinder.class);

    interface CometdMessagesTablePanelUiBinder extends UiBinder<Widget, MessagesTablePanel> { }

    public MessagesTablePanel() {
		initWidget(uiBinder.createAndBindUi(this));
        cellTable.setPageSize(cellTablePageSize);
        cellTable.addColumn(new Column<CometdMessage, String>(new TextCell()) {
            @Override
            public String getValue(CometdMessage document) {
              return "";
            }
          }, "Fecha");
        cellTable.addColumn(new Column<CometdMessage, String>(new TextCell()) {
            @Override
            public String getValue(CometdMessage document) {
            	return "";
            }
          }, "Usuario"); 
        
        cellTable.addColumn(new Column<CometdMessage, String>(new TextCell()) {
            @Override
            public String getValue(CometdMessage document) {
              //return DateUtils.getSimpleStringFromDate(user.dateOfBird);
            	return "";
            }
          }, "Contenido"); 
        
        adapter = new CometdMessageListAsyncAdapter();
        adapter.addDataDisplay(cellTable);
        //adapter.updateRowData(0, values);
        EventBusManager.getInstance().registerToCometdEvent(this);
    }
    
    protected class CometdMessageListAsyncAdapter extends AsyncDataProvider<CometdMessage>{
        @Override
        protected void onRangeChanged(HasData<CometdMessage> view) {
			Range newRange = view.getVisibleRange();
			//loadData(newRange.getStart(), newRange.getLength());
        }
    } 
    


	
    private class ServerRequestCallback implements RequestCallback {

        @Override
        public void onError(Request request, Throwable exception) {
        	new ErrorDialog().show("Exception", exception.getMessage());
        	Log.error(exception.getMessage());            
        }

        @Override
        public void onResponseReceived(Request request, Response response) {
        }
    }


	@Override
	public void onCometdEvent(CometdEvent event) {
		// TODO Auto-generated method stub
		
	}

}